package com.atlige.gzmall.search.controller;

import com.atlige.common.exception.GzmallCodeEnum;
import com.atlige.common.to.es.SkuEsModel;
import com.atlige.common.utils.R;
import com.atlige.gzmall.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Jeremy Li
 * @data 2021/3/27 - 11:46
 */
@RestController
@RequestMapping("/search")
@Slf4j
public class ElasticSaveController {
    @Autowired
    ProductSaveService productSaveService;

    /**
     * 上架商品
     * @param skuEsModels
     * @return
     */
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModels){
        boolean res = false;
        try {
            res = productSaveService.productStatusUp(skuEsModels);
        }catch (Exception e){
            log.error("ElasticSaveController商品上架错误：{}", e);
            return R.error(GzmallCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), GzmallCodeEnum.PRODUCT_UP_EXCEPTION.getMessage());
        }
        if (res) return R.ok();
        else return R.error(GzmallCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), GzmallCodeEnum.PRODUCT_UP_EXCEPTION.getMessage());
    }

}
